package com.cheng.example.atguigu.view2.client34;

import java.util.concurrent.Semaphore;

/**
 * Semaphore信号量的意思
 *
 */
public class SemaphoreDemo {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                try {
                    semaphore.acquire();//获得
                    System.out.println(Thread.currentThread().getName()+"\t抢到车位");
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName()+"\t停车三秒后离开");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();//释放
                }
            }, String.valueOf(i)).start();
        }
    }
}
